package iotwisp.school.api.controller.pc_admin.system;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.ValidationUtils;
import iotwisp.school.system.entity.Province;
import iotwisp.school.system.qo.ProvinceQO;
import iotwisp.school.system.service.ProvinceService;

@RestController
@RequestMapping("/api/system/province")
public class ProvinceController extends RestBaseController {

	@Autowired
	private ProvinceService provinceService;

	@PostMapping("/query")
	public ResponseEntity<JSONResultDTO> queryProvince(@RequestBody ProvinceQO qo) throws UnsupportedEncodingException, BusinessException {

		ValidationUtils.validate(qo);
		
		switch (qo.getResultType()) {
		case ProvinceQO.RESULT_TYPE_COUNT:
			Integer count = provinceService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case ProvinceQO.RESULT_TYPE_UNIQUE:
			Province province = provinceService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(province));
		case ProvinceQO.RESULT_TYPE_LIST:
			List<Province> provinces = provinceService.queryList(qo);
			return ResponseEntity.ok(createSuccessResult(provinces));
		case ProvinceQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = provinceService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}

	}

}
